package 博客管理系统;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.awt .*;
import javax.management.Query;
import javax.swing.JFrame;
import java.io.*;
import java.awt.event .*;
import javax.swing .*;
import javax.swing.table .*;

public class Scan_bk extends JFrame {
    JTextField jtf=new JTextField(10);
    JTextArea jta=new JTextArea(6,40);

    public Scan_bk() {
        JPanel p=new JPanel();
        JLabel lb=new JLabel("请输入用户ID:");
        JButton b1=new JButton("查询");
        b1.addActionListener(new Scan_bk.Abc1());
        JButton b2=new JButton("关闭");
        b2.addActionListener(e->System.exit(0));

        p.add(lb);
        p.add(jtf);
        p.add(b1);
        p.add(b2);
        p.add(jta);
        getContentPane().add(p);
        setTitle("博客内容查询");
        setSize(800,600);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main (String[] args) {
        new Scan_bk();
    }
    class Abc1 implements ActionListener{
        public void actionPerformed(ActionEvent e){
            String xh=jtf.getText();
            Connection con;
            Statement st;
            ResultSet rs;
            String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=BlogSystemLD";
            String userName="sa";
            String userPwd="123";
            String sql="select * from BlogContent";
            try {
                con = DriverManager.getConnection(dbURL, userName, userPwd);
                st = con.createStatement();
                rs = st.executeQuery(sql);
                while (rs.next()) {
                    String id = rs.getString(1);
                    String content = rs.getString(2);
                    String blog_title = rs.getString(3);
                    String category= rs.getString(4);
                    String create_time= rs.getString(5);
                    String beats = rs.getString(6);
                    int num=Integer.parseInt(beats);
                    if(xh.equals(id))
                        jta.append("用户ID"+id+"\r\n");
                    jta.append("博客内容"+content+"\r\n");
                    jta.append("博客标题"+blog_title+"\r\n");
                    jta.append("博客类别"+category+"\r\n");
                    jta.append("创建时间"+create_time+"\r\n");
                    jta.append("点赞次数"+num+"\r\n");
                }
                rs.close();
                st.close();
                con.close();
            }catch(SQLException exception){
                exception.printStackTrace();
                System.out.println("数据库连接失败！！！");
            }

        }
    }
}
